/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ztewatch.wearcontacts.loader;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;

import com.ztewatch.wearcontacts.R;

/**
 * A loader for use in the default contact list, which will also query for the
 * user's group if configured to do so.
 */
public class ContactsListLoader extends CursorLoader {
	public static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
			Data._ID, // 0
			Email.TYPE, // 1
			Email.LABEL, // 2
			Email.DATA, // 3
			Data.PHOTO_ID, // 4
			Data.DISPLAY_NAME_PRIMARY, // 5
			Data.MIMETYPE, // 6
			RawContacts.ACCOUNT_NAME, // 7
			Data.PHOTO_THUMBNAIL_URI,// 8
			Data.LOOKUP_KEY, // 9
			Data.RAW_CONTACT_ID//10
	};

	public static final int COLUMN_PHOTO_ID = 4;
	public static final int COLUME_PHOTO_URI = 8;
	public static final int COLUMN_LOOKUP_KEY = 9;
	public static final int ID_COLUMN_ID = 0;
	public static final int NAME_COLUMN_ID = 5;
	public static final int NUMBER_COLUMN_ID = 3;
	public static final int RAW_CONTACT_ID = 10;
	
	private Context mContext;

	public ContactsListLoader(Context context) {
		super(context);
		mContext = context;
	}

	public void setLoadProfile(boolean flag) {
	}

	public void setProjection(String[] projection) {
		super.setProjection(projection);
	}

	@Override
	public Cursor loadInBackground() {
		List<Cursor> cursors = new ArrayList<Cursor>();
		Cursor cursor = null;
		try {
			cursor = super.loadInBackground();
		} catch (NullPointerException e) {
		}

		// add history start
		MatrixCursor matrix = new MatrixCursor(CONTACTS_SUMMARY_PROJECTION);
		Object[] addHistory = new Object[CONTACTS_SUMMARY_PROJECTION.length];
		for (int i = 0; i < addHistory.length; i++) {
			if (Data.DISPLAY_NAME_PRIMARY
					.equals(CONTACTS_SUMMARY_PROJECTION[i])) {
				addHistory[i] = mContext.getString(R.string.history_str);
			}
		}
		matrix.addRow(addHistory);
		// add history end
		Object[] row = new Object[CONTACTS_SUMMARY_PROJECTION.length];
		while (cursor.moveToNext()) {
			for (int i = 0; i < row.length; i++) {
				row[i] = cursor.getString(i);
			}
			matrix.addRow(row);
		}

		cursors.add(matrix);
		return new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
	}
}
